﻿<UserControl x:Class="DataForest.View.TableView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:converter="clr-namespace:DataForest.View.Converter"
             mc:Ignorable="d" d:DesignWidth="613" d:DesignHeight="367" x:Name="tableView">
    <UserControl.Resources>
        <converter:IsTargetColumConverter x:Key="IsTargetColumConverter" />
    </UserControl.Resources>
    
    <Grid Name="grid1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="329*" />
            <RowDefinition Height="32" />
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="1" ItemsSource="{Binding Group.Table.DefaultView, UpdateSourceTrigger=PropertyChanged, IsAsync=True}" CurrentCell="{Binding SelectedCell, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  Margin="0,0,0,0" MinColumnWidth="60" SelectionMode="Single" SelectionUnit="Cell" CanUserDeleteRows="True">
            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource IsTargetColumConverter}">
                                    <Binding RelativeSource="{RelativeSource Self}"/>
                                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}" Path="DataContext.Group.Table.TargetAttribute" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Background" Value="AliceBlue" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                    <Setter Property="Height" Value="30" />
                    <Setter Property="FontWeight" Value="Black" />
                    <Setter Property="Tag" Value="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" />
                    <!--<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Self}}" />-->
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Header="Als Zielattribut setzen" Command="{Binding PlacementTarget.Tag.SetTargetAttributCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu, Mode=FindAncestor}}">
                                    <MenuItem.Icon>
                                        <Image Source="Icons/target-attribut.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Löschen" Command="{Binding PlacementTarget.Tag.DeleteColCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu, Mode=FindAncestor}}">
                                    <MenuItem.Icon>
                                        <Image Source="Icons/table-delete-col.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Spalte links einfügen" Command="{Binding PlacementTarget.Tag.InsertColBeforeCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu, Mode=FindAncestor}}">
                                    <MenuItem.Icon>
                                        <Image Source="Icons/table-insert-column-before.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Spalte rechts einfügen" Command="{Binding PlacementTarget.Tag.InsertColAfterCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}" CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu, Mode=FindAncestor}}">
                                    <MenuItem.Icon>
                                        <Image Source="Icons/table-insert-column-after.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource IsTargetColumConverter}">
                                    <Binding RelativeSource="{RelativeSource Self}" />
                                    <Binding RelativeSource="{RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}" Path="DataContext.Group.Table.TargetAttribute" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="ContentTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="Icons/target-attribut.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" Margin="0,0,5,0" />
                                            <TextBlock Text="{Binding}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                    <Setter Property="Height" Value="20" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Header="Zeile Löschen" Command="{Binding DataContext.DeleteRowCommand, Source={x:Reference tableView}}" CommandParameter="{Binding}">
                                    <MenuItem.Icon>
                                        <Image Source="Icons/table-delete-row.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Zeile oberhalb einfügen" Command="{Binding DataContext.InsertRowBeforeCommand, Source={x:Reference tableView}}" CommandParameter="{Binding}">
                                    <MenuItem.Icon>
                                        <Image Source="Icons/table-insert-row-before.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Zeile unterhalb einfügen" Command="{Binding DataContext.InsertRowAfterCommand, Source={x:Reference tableView}}" CommandParameter="{Binding}">
                                    <MenuItem.Icon>
                                        <Image Source="Icons/table-insert-row-after.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.Resources>
        </DataGrid>
        <Button Grid.Row="2" Height="23" Margin="0,4,4,0" Name="button2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="97" Command="{Binding DataContext.CreateOptimalTreeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}">
            <StackPanel Orientation="Horizontal">
                <Image Source="Icons/tree-auto.png"></Image>
                <TextBlock>Auto</TextBlock>
            </StackPanel>
        </Button>
        <Button Grid.Row="2" Height="23" Margin="0,4,106,0" Name="button3" VerticalAlignment="Top" HorizontalAlignment="Right" Width="91" Command="{Binding DataContext.CreateInteractiveTreeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}">
            <StackPanel Orientation="Horizontal">
                <Image Source="Icons/tree.png"></Image>
                <TextBlock><Run Text="Interaktiv"/></TextBlock>
            </StackPanel>
        </Button>
        <ToolBarPanel Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Stretch" Orientation="Horizontal">
            <ToolBar Height="30" HorizontalAlignment="Left">
                <Button Width="25" Height="25" Command="{Binding DataContext.SaveCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}">
                    <Image Source="Icons/document-save.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.SaveAsCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}">
                    <Image Source="Icons/document-save-as.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Separator />
                <Button Width="25" Height="25" Command="{Binding DataContext.InsertColAfterCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}">
                    <Image Source="Icons/table-insert-column-after.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.InsertColBeforeCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}">
                    <Image Source="Icons/table-insert-column-before.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.InsertRowAfterCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}">
                    <Image Source="Icons/table-insert-row-after.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.InsertRowBeforeCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}">
                    <Image Source="Icons/table-insert-row-before.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.DeleteColCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}">
                    <Image Source="Icons/table-delete-col.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.DeleteRowCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}">
                    <Image Source="Icons/table-delete-row.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.RenameColCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}">
                    <Image Source="Icons/attribut-rename.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Separator />
                <Button Width="25" Height="25" Command="{Binding DataContext.SetTargetAttributCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}">
                    <Image Source="Icons/target-attribut.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.CreateOptimalTreeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}">
                    <Image Source="Icons/tree-auto.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
                <Button Width="25" Height="25" Command="{Binding DataContext.CreateInteractiveTreeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}">
                    <Image Source="Icons/tree.png" Stretch="Fill" Width="18" Height="18" RenderOptions.BitmapScalingMode="HighQuality" />
                </Button>
            </ToolBar>
        </ToolBarPanel>
    </Grid>
</UserControl>
